package com.example.controller;

import com.example.bean.Goods;
import com.example.bean.ResultVo;
import com.example.config.Result;
import com.example.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@RestController
public class GoodsController {

    @Autowired
    GoodsService goodsService;

    /**
     * 删除给定ID的商品
     *
     * @param gId
     * @return
     */
    @RequestMapping("/api/deleteGoods")
    @CrossOrigin
    public Result deleteGoods(@RequestParam("gId") int gId) {
        goodsService.deleteById(gId);
        return Result.success();
    }


    /**
     * 新增商品
     *
     * @return
     */
    @RequestMapping("/api/addGoods")
    @CrossOrigin
    public void addGoods(@RequestParam(value = "gId") int gId,
                         @RequestParam(value = "mId") int mId,
                         @RequestParam(value = "gName") String gName,
                         @RequestParam(value = "price") float price,
                         @RequestParam(value = "gDescribe") String gDescribe,
                         @RequestParam(value = "gInventory") int gInventory,
                         @RequestParam(value = "file") MultipartFile file) {


        String imgFile = null;

        //生成新的文件名
        String newFile = UUID.randomUUID() + "-" + file.getOriginalFilename();
        //使用新的文件名构建文件对象
        File f = new File("d:/image", newFile);
        //判断上级目录是否存在，不存在则创建
        if (!f.getParentFile().exists()) {
            f.mkdirs();
        }
        try {
            //将上传的文件储存到指定的磁盘位置
            file.transferTo(f);
            //记录上传文件的web访问路径/123
            imgFile = "http://localhost/upload/" + newFile;
        } catch (IOException e) {
            e.printStackTrace();
        }


        String gImg = imgFile;
        String gState = "上架";
        //封装到goods中
        Goods goods = new Goods(gId, mId, gName, price, gDescribe, gInventory, gImg, gState);
        goodsService.insertGoods(goods);
    }

    /**
     * 商品修改
     *
     * @param gId
     * @param mId
     * @param gName
     * @param price
     * @param gDescribe
     * @param gInventory
     * @param file
     */
    @RequestMapping("/api/updateImgGoods")
    @CrossOrigin
    public void updateImgGoods(@RequestParam(value = "gId") int gId,
                               @RequestParam(value = "mId") int mId,
                               @RequestParam(value = "gName") String gName,
                               @RequestParam(value = "price") float price,
                               @RequestParam(value = "gDescribe") String gDescribe,
                               @RequestParam(value = "gInventory") int gInventory,
                               @RequestParam(value = "file") MultipartFile file,
                               @RequestParam(value = "gState") String gState) {

        String imgFile = null;

        //生成新的文件名
        String newFile = UUID.randomUUID() + "-" + file.getOriginalFilename();
        //使用新的文件名构建文件对象
        File f = new File("d:/image", newFile);
        //判断上级目录是否存在，不存在则创建
        if (!f.getParentFile().exists()) {
            f.mkdirs();
        }
        try {
            //将上传的文件储存到指定的磁盘位置
            file.transferTo(f);
            //记录上传文件的web访问路径/123
            imgFile = "http://localhost/upload/" + newFile;
        } catch (IOException e) {
            e.printStackTrace();
        }


        String gImg = imgFile;

        //封装到goods中
        Goods goods = new Goods(gId, mId, gName, price, gDescribe, gInventory, gImg, gState);
        goodsService.updateImgGoods(goods);
    }

    @RequestMapping("/api/updateGoods")
    @CrossOrigin
    public void updateGoods(@RequestParam(value = "gId") int gId,
                            @RequestParam(value = "mId") int mId,
                            @RequestParam(value = "gName") String gName,
                            @RequestParam(value = "price") float price,
                            @RequestParam(value = "gDescribe") String gDescribe,
                            @RequestParam(value = "gInventory") int gInventory,
                            @RequestParam(value = "gImg") String gImg,
                            @RequestParam(value = "gState") String gState){

        Goods goods = new Goods(gId, mId, gName, price, gDescribe, gInventory, gImg, gState);
        goodsService.updateImgGoods(goods);

    }

    /**
     * 查询商家所属的所有商品
     *
     * @return
     */
    @RequestMapping("/api/selectAllGoods")
    @CrossOrigin
    public ResultVo selectAllGoods(@RequestParam("mId") int mId,
                                   @RequestParam("gName") String gName) {
        String likeName = "%" + gName + "%";
        return goodsService.selectAllGoods(mId, likeName);
    }

    /**
     * 用户查询商家所属的所有商品
     *
     * @return
     */
    @RequestMapping("/api/UserSelectAllGoods")
    @CrossOrigin
    public ResultVo selectAllGoods(@RequestParam("mId") int mId) {
        return goodsService.UserSelectAllGoods(mId);
    }



    /**
     * 分页查询所有商品
     * @param gName 前台搜索框中传过来的值
     * @param currentPage 当前页码
     * @param currentCount  一页显示的条数
     * @return
     */
    @RequestMapping("/api/selectTotalGoods")
    @CrossOrigin
    public ResultVo selectTotalGoods(@RequestParam("gName")String gName,
                                     @RequestParam("currentPage")int currentPage,
                                     @RequestParam("currentCount")int currentCount) {
        return goodsService.selectTotalGoods(gName,currentPage,currentCount);
    }

    /**
     * 后台搜索框查询商品
     *
     * @param gName
     * @return
     */
    @RequestMapping("/api/selectLikeName")
    @CrossOrigin
    public ResultVo selectLikeName(@RequestParam(value = "gName") String gName,
                                   @RequestParam(value = "mId") Integer mId) {
        String likeName = "%" + gName + "%";
        return goodsService.selectLikeName(mId, likeName);
    }


    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequestMapping("/api/selectPageGoods")
    @CrossOrigin
    public ResultVo findPage(@RequestParam(value = "pageNum") Integer pageNum,
                             @RequestParam(value = "pageSize") Integer pageSize,
                             @RequestParam(value = "mId") Integer mId,
                             @RequestParam(value = "gName") String gName) {
        String likeName = "%" + gName + "%";
        return goodsService.findpage(mId, pageNum, pageSize, likeName);
    }

    /**
     * 通过gId查询商品
     *
     * @param gId
     * @return
     */
    @RequestMapping("/api/selectBygId")
    @CrossOrigin
    public ResultVo selectBymName(@RequestParam("gId") int gId) {
        return goodsService.selectBygId(gId);
    }

    /**
     * 商品状态更换
     *
     * @param gId
     * @param gState
     */
    @RequestMapping("/api/updateGoodsState")
    @CrossOrigin
    public void updateGoodsState(@RequestParam("gId") int gId,
                                 @RequestParam("gState") String gState) {
        goodsService.updateGoodsState(gId, gState);
    }

    /**
     * 查询商品图片
     * @return
     */
    @RequestMapping("/api/selectImgs")
    @CrossOrigin
    public ResultVo selectGoodImgs(){
        List<String> goodImgs=goodsService.selectGoodImgs();
        return new ResultVo(goodImgs);
    }

    /**
     * 查询在架的全部的商品的数量
     * @param gName 商品名，如果为空，则查询全部
     * @return
     */
    @RequestMapping("/api/selectTotalGoodsCount")
    @CrossOrigin
    public ResultVo selectTotalGoodsCount(@RequestParam("gName")String gName){
        return new ResultVo(goodsService.selectTotalGoodsCount(gName));
    }
}
